﻿#include "uimodelinfo.h"
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <ElaText.h>
#include <QFrame>
#include <QTabWidget>
#include <QFormLayout>
#include <QComboBox>

#include <ElaTabWidget.h>
#include <ElaWidget.h>
#include <ElaText.h>


#include <QStackedWidget>
#include <QLabel>


#include <model/uimodeltreeview.h>
#include <protocol/modbus/uimodbusmapinfo.h>
#include <shlib/cwebrequestapi.h>
#include <model/device/uideviceinfo.h>


UiModelInfo::UiModelInfo(QWidget *parent) : QWidget{parent}
{
    uiInit();
}

void UiModelInfo::uiInit() {
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setStyleSheet("background-color: rgba(0, 0, 0, 0);");

    QHBoxLayout * nameLayout = new QHBoxLayout;
    ElaText* leb_name_temp = new ElaText("模型名称:");
    QFont font = leb_name_temp->font();
    font.setPointSize(14);
    QFont font_1 = leb_name_temp->font();
    font_1.setPointSize(13);
    leb_name_temp->setFont(font);
    m_lab_name = new  ElaText;
    m_lab_name->setFont(font_1);
    nameLayout->addWidget((QWidget*)leb_name_temp, 1);
    nameLayout->addWidget((QWidget*)m_lab_name, 6);

    QHBoxLayout * userLayout = new QHBoxLayout;
    ElaText* leb_user_temp = new ElaText("所属用户:");
    leb_user_temp->setFont(font);
    m_lab_user = new ElaText;
    m_lab_user->setFont(font_1);
    userLayout->addWidget((QWidget*)leb_user_temp, 1);
    userLayout->addWidget((QWidget*)m_lab_user, 6);

    QHBoxLayout * descLayout = new QHBoxLayout;
    ElaText* leb_desc_temp = new ElaText("模型描述:");
    leb_desc_temp->setFont(font);
    m_lab_descr = new ElaText;
    m_lab_descr->setFont(font_1);
    descLayout->addWidget((QWidget*)leb_desc_temp, 1);
    descLayout->addWidget((QWidget*)m_lab_descr, 6);

    QHBoxLayout * snLayout = new QHBoxLayout;
    ElaText* leb_sn_temp = new ElaText("模型排序:");
    leb_sn_temp->setFont(font);
    m_lab_sn = new  ElaText;
    m_lab_sn->setFont(font_1);
    snLayout->addWidget((QWidget*)leb_sn_temp, 1);
    snLayout->addWidget((QWidget*)m_lab_sn, 6);

    QHBoxLayout * createtimeLayout = new QHBoxLayout;
    ElaText* leb_createtime_temp = new ElaText("创建时间:");
    leb_createtime_temp->setFont(font);
    m_lab_createtime = new  ElaText;
    m_lab_createtime->setFont(font_1);
    createtimeLayout->addWidget((QWidget*)leb_createtime_temp, 1);
    createtimeLayout->addWidget((QWidget*)m_lab_createtime, 6);

    QHBoxLayout * updatetimeLayout = new QHBoxLayout;
    ElaText* leb_updatetime_temp = new ElaText("更新时间:");
    leb_updatetime_temp->setFont(font);
    m_lab_updatetime = new ElaText;
    m_lab_updatetime->setFont(font_1);
    updatetimeLayout->addWidget((QWidget*)leb_updatetime_temp, 1);
    updatetimeLayout->addWidget((QWidget*)m_lab_updatetime, 6);

    QHBoxLayout * groupLayout = new QHBoxLayout;
    ElaText* leb_group_temp = new ElaText("模型分组:");
    leb_group_temp->setFont(font);
    m_lab_group = new ElaText;
    m_lab_group->setFont(font_1);
    groupLayout->addWidget((QWidget*)leb_group_temp, 1);
    groupLayout->addWidget((QWidget*)m_lab_group, 6);

    QHBoxLayout * producerLayout = new QHBoxLayout;
    ElaText* leb_producer_temp = new ElaText("出厂型号:");
    leb_producer_temp->setFont(font);
    m_lab_producer = new ElaText;
    m_lab_producer->setFont(font_1);
    producerLayout->addWidget((QWidget*)leb_producer_temp, 1);
    producerLayout->addWidget((QWidget*)m_lab_producer, 6);

    QHBoxLayout * locationLayout = new QHBoxLayout;
    ElaText* leb_location_temp = new ElaText("安装位置:");
    leb_location_temp->setFont(font);
    m_lab_location = new ElaText;
    m_lab_location->setFont(font_1);
    locationLayout->addWidget((QWidget*)leb_location_temp, 1);
    locationLayout->addWidget((QWidget*)m_lab_location, 6);

    QHBoxLayout * validLayout = new QHBoxLayout;
    ElaText* leb_valid_temp = new ElaText("是否启用:");
    leb_valid_temp->setFont(font);
    m_lab_valid = new ElaText;
    m_lab_valid->setFont(font_1);
    validLayout->addWidget((QWidget*)leb_valid_temp, 1);
    validLayout->addWidget((QWidget*)m_lab_valid, 6);

    QFormLayout* infoCardLayout = new QFormLayout;
    infoCardLayout->setSpacing(20);

    QHBoxLayout *tempLayout = new QHBoxLayout;
    tempLayout->addLayout(nameLayout, 1);
    tempLayout->addLayout(userLayout, 1);
    infoCardLayout->addRow(tempLayout);

    tempLayout = new QHBoxLayout;
    tempLayout->addLayout(descLayout, 1);
    tempLayout->addLayout(snLayout, 1);
    infoCardLayout->addRow(tempLayout);

    tempLayout = new QHBoxLayout;
    tempLayout->addLayout(createtimeLayout, 1);
    tempLayout->addLayout(updatetimeLayout, 1);
    infoCardLayout->addRow(tempLayout);

    tempLayout = new QHBoxLayout;
    tempLayout->addLayout(groupLayout, 1);
    tempLayout->addLayout(producerLayout, 1);
    infoCardLayout->addRow(tempLayout);

    tempLayout = new QHBoxLayout;
    tempLayout->addLayout(locationLayout, 1);
    tempLayout->addLayout(validLayout, 1);
    infoCardLayout->addRow(tempLayout);

    QWidget* infowidget = new QWidget;
    infowidget->setWindowFlags(Qt::FramelessWindowHint);
    infowidget->setStyleSheet("background-color: rgba(0, 0, 0, 0);");
    infowidget->setLayout(infoCardLayout);

    m_maintabWidget = new ElaTabWidget(this);
    m_maintabWidget->setTabsClosable(false);
    m_maintabWidget->setIsTabTransparent(true);


    m_tabWidget = new ElaTabWidget(this);
    m_tabWidget->setTabsClosable(false);
    m_tabWidget->setIsTabTransparent(true);

    m_yxview = new UiModelYxTableView(m_tabWidget);
    connect(this, &UiModelInfo::selectModelChange, m_yxview, &UiModelYxTableView::reload);
    m_tabWidget->addTab(m_yxview, "遥信");

    m_ycview = new UiModelYcTableView(m_tabWidget);
    connect(this, &UiModelInfo::selectModelChange, m_ycview, &UiModelYcTableView::reload);
    m_tabWidget->addTab(m_ycview, "遥测");

    m_ykview = new UiModelYkTableView(m_tabWidget);
    connect(this, &UiModelInfo::selectModelChange, m_ykview, &UiModelYkTableView::reload);
    m_tabWidget->addTab(m_ykview, "遥控");

    m_parameterview = new UiModelParameterTableView(m_tabWidget);
    connect(this, &UiModelInfo::selectModelChange, m_parameterview, &UiModelParameterTableView::reload);
    m_tabWidget->addTab(m_parameterview, "参数");

    QVBoxLayout* contentLayout = new QVBoxLayout;
    contentLayout->addWidget(infowidget, 1);
    contentLayout->addWidget(m_tabWidget, 5);

    QWidget* tmpwidget = new QWidget(this);
    tmpwidget->setWindowFlags(Qt::FramelessWindowHint);
    tmpwidget->setStyleSheet("background-color: rgba(0, 0, 0, 0);");
    tmpwidget->setLayout(contentLayout);

    m_modbusmapwidget = new UiModBusMapInfo(this);
    m_modbusmapwidget->setWindowFlags(Qt::FramelessWindowHint);
    m_modbusmapwidget->setStyleSheet("background-color: rgba(0, 0, 0, 0);");
    connect(this, &UiModelInfo::selectModelChange, m_modbusmapwidget, &UiModBusMapInfo::slotSelectModelChange);

    m_deviceView = new UiDeviceInfo(this);
    connect(this, &UiModelInfo::selectModelChange, m_deviceView, &UiDeviceInfo::slotSelectModelChange);

    m_maintabWidget->addTab(tmpwidget, "模型信息");
    m_maintabWidget->addTab(m_modbusmapwidget, "modbus通讯映射");
    m_maintabWidget->addTab(m_deviceView, "设备管理");

    m_modelTree = new UiModelTreeView(this);

    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->addWidget(m_modelTree, 1);
    mainLayout->addWidget(m_maintabWidget, 6);

    connect(m_modelTree, &UiModelTreeView::sigSelectModelChange, this, &UiModelInfo::onSigSelectModelChange);
}

void UiModelInfo::onSigSelectModelChange(QSharedPointer<CModelInfo> &model)
{
    if(!m_info){
        m_info = model;
    } else {
        if(model->name == m_info->name)
            return;
        m_info = model;
    }

    m_lab_name->setText(m_info->name);
    m_lab_user->setText(m_info->user);
    m_lab_descr->setText(m_info->descr);
    m_lab_sn->setText(QString::number(m_info->sn));
    m_lab_group->setText(m_info->grouping);
    m_lab_location->setText(m_info->location);
    m_lab_createtime->setText(m_info->create_time.toString());
    m_lab_updatetime->setText(m_info->update_time.toString());
    m_lab_producer->setText(m_info->producer);
    m_lab_valid->setText(m_info->valid ? "启用" : "禁用");
    emit selectModelChange(m_info->name, m_info->user);
}
